/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package controller;

import ModeratorOperations.ModeratorManagePosts;
import Utility.PostOperationsUtility;
import Utility.ThreadOperationsUtility;
import Utility.Utility;
import beans.ManagePostBean;
import beans.UserType;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import xmlMapping.Forum;
import xmlMapping.Post;
import xmlMapping.Thread;

/**
 *
 * @author sh
 */
public class ManagePostAction extends org.apache.struts.actions.DispatchAction {
    
    /* forward name="success" path="" */
    private static final String SUCCESS = "success";
    
    /**
     * This is the action called from the Struts framework.
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @throws java.lang.Exception
     * @return
     */
//    @Override
//    public ActionForward execute(ActionMapping mapping, ActionForm form,
//            HttpServletRequest request, HttpServletResponse response)
//            throws Exception {
//
//        return mapping.findForward(SUCCESS);
//    }

     public ActionForward mergePost(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
         String find;
         UserType type=(UserType)request.getSession().getAttribute("type");
           Integer[] merge=((ManagePostBean)form).getMerge();
         String title=((ManagePostBean)form).getTitle();
         ActionErrors errors = new ActionErrors();
        if(type==null||(!type.equals(UserType.adm)&&!type.equals(UserType.mod)&&!type.equals(UserType.supermod))){
//            System.out.println("in manaxxxxxxxxxxxxxxxxxxxx xxxxxxxxxxxxxxxxxx"+type);
            find="login";
            //return mapping.findForward("login");
        }else{
             if(merge==null||title==null||merge.length!=2||title.equals("")){
                errors.add("size", new ActionMessage("errorpost","num of post OR Not add title !"));
                saveErrors(request, errors);
                find="error";
                //return mapping.findForward("error");
           }else{
                 ModeratorManagePosts modmanagepost=new ModeratorManagePosts();
                 modmanagepost.MergePosts(merge[0], merge[1], title);
                 request.setAttribute("threadid", request.getParameter("thId"));
                 find="mergepost";
           }
        }

         int pagenumber=Integer.parseInt(request.getParameter("pagenumber"));
         request.setAttribute("pagenumber", pagenumber);
        return mapping.findForward(find);
    }

      public ActionForward preSplitpost(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {

                String find;

                UserType type=(UserType)request.getSession().getAttribute("type");
                if(type==null||(!type.equals(UserType.adm)&&!type.equals(UserType.mod)&&!type.equals(UserType.supermod))){
                    find="login";
                }else{
                    int pId=Integer.parseInt(request.getParameter("postid"));
                    Post post=PostOperationsUtility.getPost(pId);
                    request.setAttribute("onepost", post);
                    request.setAttribute("threadid", request.getParameter("thId"));
                    find="presplitpage";
                }
                int pagenumber=Integer.parseInt(request.getParameter("pagenumber"));
                request.setAttribute("pagenumber", pagenumber);

                return mapping.findForward(find);
      }

      public ActionForward preMovepost(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {

                    String find;

                UserType type=(UserType)request.getSession().getAttribute("type");
                if(type==null||(!type.equals(UserType.adm)&&!type.equals(UserType.mod)&&!type.equals(UserType.supermod))){
                    find="login";
                }else{
                    int thId=Integer.parseInt(request.getParameter("thId"));
                    int pId=Integer.parseInt(request.getParameter("postid"));
                    Thread thread=ThreadOperationsUtility.getThread(thId);
                    Utility utility=new Utility();
                    Forum forum=new Forum();
                    forum.setThreads(new HashSet(utility.getThreads(((Forum)thread.getForum()).getForumId())));
                    //utility.getThreads(((Forum)thread.getForum()).getForumId());
                    Post post=PostOperationsUtility.getPost(pId);
                    request.setAttribute("forums", forum);
                    request.setAttribute("onepost", post);
                    request.setAttribute("threadid", thId);
                    find="premovepage";
                }

                int pagenumber=Integer.parseInt(request.getParameter("pagenumber"));
                request.setAttribute("pagenumber", pagenumber);
             return mapping.findForward(find);
      }
}
